package com.hk.controller;

import com.hk.entity.CourseInfo;
import com.hk.entity.PageBean;
import com.hk.entity.Result;
import com.hk.entity.Teacher;
import com.hk.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教师表控制层（emp）
 * @since 2024-11-11 14:12:04
 */
@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 增加教师
     * @param teacher
     * @return
     */
    @PostMapping("/add")
    public Result TeacherAdd(@RequestBody Teacher teacher){
        log.info("增加教师：teacher：{}",teacher);
//        if (teacher.getEmpId() == null) {
//            throw new IllegalArgumentException("教师 ID 不能为空");
//        }
        teacherService.teacherAdd(teacher);
        return Result.success();
    }

    /**
     * 批量删除教师
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result TeacherDelete(@PathVariable List<Integer> ids){
        log.info("批量删除教师：ids：{}",ids);
        teacherService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询教师，查询回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询教师：id: {}",id);
        Teacher teacher = teacherService.getById(id);
        return Result.success(teacher);
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Result TeacherUpdate(@RequestBody Teacher teacher){
        if (teacher.getId() == null) {
            throw new IllegalArgumentException("教师 id " +
                    "不能为空");
        }
        log.info("更新教师信息：stu: {}",teacher);
        teacherService.update(teacher);
        return Result.success();
    }

    /**
     * 查询所有教师
     * @return
     */
   // 允许管理员或者学生角色访问
    @GetMapping("/queryAll")
    public Result queryAll(){
        log.info("查询所有教师：");
        List<Teacher> teacherList = teacherService.queryAll();
        return Result.success(teacherList);
    }

    /**
     * 搜索教师，根据编号或姓名查询,模糊匹配
     * @param teacher
     * @return
     */
    @GetMapping("/searchTea")
    public Result search(@ModelAttribute Teacher teacher){
        log.info("搜索教师，根据编号或姓名查询：teacher:{}",teacher);
        List<Teacher> search = teacherService.searchTea(teacher);
        return Result.success(search);
    }

    /**
     * 分页查询，教师
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("教师：分页查询：参数：{}，{}",page,pageSize);
        PageBean pageBean = teacherService.page(page,pageSize);
        return Result.success(pageBean);
    }

    /**
     * 添加课程信息
     * @param id 传入的教师xu号
     * @param courseInfo
     * @return
     */
    @PutMapping("/info/addCourse/{empId}")
    public Result addCourse(@PathVariable Integer id ,@RequestBody CourseInfo courseInfo){
        log.info("增加课程信息：{},{}", id,courseInfo);
        log.info("检查参数：major={}, className={}, term={}, course={}",
                courseInfo.getMajor(), courseInfo.getClassName(), courseInfo.getTerm(), courseInfo.getCourse());
        if (courseInfo.getMajor() == null || courseInfo.getClassName() == null || courseInfo.getTerm() == null || courseInfo.getCourse() == null) {
            throw new IllegalArgumentException("专业、班级、学期和课程不能为空");
        }
        // 将教师ID与课程信息关联
        courseInfo.setEmpId(id);
        teacherService.addCourse(courseInfo);
        return Result.success("添加课程信息成功！");
    }

    /**
     * 根据id查询授课，查询回显
     * @param empId
     * @return
     */
    @GetMapping("/info/empId")
    public Result getByIdInfo(@RequestParam("empId") String empId){
        log.info("根据ID查询教师授课：empId: {}",empId);
        List<CourseInfo> courseInfoList= teacherService.getByIdInfo(empId);
        return Result.success(courseInfoList);
    }

    /**
     * 删除授课信息，可多选删除
     * @param ids
     * @return
     */
    @DeleteMapping("/info/delete/{ids}")
    public Result DeleteInfo(@PathVariable List<Integer> ids) {
        log.info("删除授课信息：参数：{}", ids);
        teacherService.deleteInfo(ids);
        return Result.success();
    }

    /**
     * 更新授课信息,根据id来进行更改
     * @param courseInfo
     * @return
     */
    @PutMapping("/info/updata")
    public Result UpdataInfo(@RequestBody CourseInfo courseInfo){
        log.info("更新授课信息：courseInfo: {}",courseInfo);
        teacherService.UpdataInfo(courseInfo);
        return Result.success();
    }



}
